﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Meraz.Core.Data;
using Meraz.Core.Model;
using Meraz.Core.Repositories;
using Meraz.Core.Parsers;
using Meraz.Core.Managers;
using Meraz.Core.Helpers;
using Ninject;

namespace Meraz.Core.Commands
{
    public interface ICreatePostCommand
    {
        ILinkRepository LinkRepository { get; set; }
        IPosterRepository PosterRepository { get; set; }
        Post Execute(PostValues values, List<Alias> aliases, List<Url> urls);
    }
    public class CreatePostCommand : BaseCommand, ICreatePostCommand
    {

        [Inject]
        public ILinkRepository LinkRepository { get; set; }
        [Inject]
        public IPosterRepository PosterRepository { get; set; }
        public Post Execute(PostValues values, List<Alias> aliases, List<Url> urls)
        {
            Post post = new Post();
            post.Posters = new List<Poster>();
            post.Links = new List<Link>();
            if (aliases != null)
            {
                foreach (Alias alias in aliases)
                {
                    Poster poster = new Poster();
                    PosterRepository.Add(poster);
                    poster.Alias = alias;
                    poster.Post = post;
                    post.Posters.Add(poster);
                }
            }
            if (urls != null)
            {
                foreach (Url url in urls)
                {
                    Link link = new Link();
                    LinkRepository.Add(link);
                    link.Post = post;
                    link.Url = url;
                    link.ToneType = ToneType.Undefined;
                    post.Links.Add(link);
                }
            }
            post.BlogName = values.BlogName;
            PostBody body = new PostBody();
            body.Name = values.Body;
            body.Post = post;
            post.Body = body;
            post.Permalink = values.Permalink;
            post.Pubdate = values.Pubdate;
            post.Title = values.Title;

            Commit();
            return post;
        }
    }
}
